define([], function() {
    var app = angular.module("App.home");
    app.controller("projects.detail.pointsCtrl", ["$scope", "$q", "$timeout", "curPrj", "showMsg", function($scope, $q, $timeout, curPrj, showMsg) {

        $scope.currentType = 2;

        /* 
         * 手动上传
         */
        $scope.prefixTypes = ["SX", "XX", "SP", "XP"];
        $scope.locationTypes = ["上行线", "下行线", "出入口"];
        $scope.monitorTypes = null;
        $scope.dict = null;
        $scope.originDict = null;
        var getDictionary = function() {
            $q.all([curPrj.getMonitorTypes(), curPrj.getMonitorPointsDictionary()])
                .then(function(data) {
                    $scope.monitorTypes = data[0];
                    $scope.originDict = data[1];
                    $scope.dict = angular.copy($scope.originDict);
                    $scope.dict.forEach(function(item) {
                        if ($scope.prefixTypes.indexOf(item.Prefix) === -1) {
                            $scope.prefixTypes.push(item.Prefix);
                        }
                        if ($scope.locationTypes.indexOf(item.Location) === -1) {
                            $scope.locationTypes.push(item.Location);
                        }
                    });
                });
        };

        $scope.reset = function() {
            $scope.dict = angular.copy($scope.originDict);
        };

        $scope.delete = function() {
            $scope.dict = $scope.dict.filter(function(item) {
                return !item.Selected;
            });
        };

        $scope.add = function() {
            var item = {
                Prefix: "",
                Start: 1,
                End: 1,
                Location: "",
                MonitorType: $scope.monitorTypes[0],
                PrjId: curPrj.prjId
            };
            $scope.dict.push(item);
        };

        $scope.confirm = function() {
            $scope.status = "uploading";
            curPrj
                .postMonitorPointsDictionary($scope.dict)
                .then(function() {
                    $scope.status = "succeeded";
                }, function() {
                    $scope.status = "failed";
                });
        };

        getDictionary();

        /* 
         * Excel上传
         */
        var initUploader = function() {
            var uploader = Qiniu.uploader({
                runtimes: 'html5,flash,html4', // 上传模式，依次退化
                browse_button: 'excelUpload', // 上传选择的点选按钮，**必需**
                uptoken_url: '/token',
                unique_names: true,
                filter: {
                    mime_types: [{
                        title: "Excel Files",
                        extensions: "xls,xlsx"
                    }]
                },
                domain: 'http://7i7il6.com1.z0.glb.clouddn.com/',
                container: 'container', // 上传区域DOM ID，默认是browser_button的父元素
                max_file_size: '100mb', // 最大文件体积限制
                flash_swf_url: 'javascripts/bin/plupload/Moxie.swf', // 引入flash，相对路径
                max_retries: 3, // 上传失败最大重试次数
                dragdrop: false, // 开启可拖拽上传
                chunk_size: '4mb', // 分块上传时，每片的体积
                auto_start: true, // 选择文件后自动上传，若关闭需要自己绑定事件触发上传
                init: {
                    'FilesAdded': function(up, files) {
                        if (files.length > 1) {
                            showMsg("只允许上传一个文件！", "alert");
                            return;
                        }
                    },
                    'BeforeUpload': function(up, file) {
                        $scope.status = "uploading";
                    },
                    'FileUploaded': function(up, file, info) {
                        var domain = up.getOption('domain');
                        info = JSON.parse(info);
                        var uploadItem = {
                            prjId: curPrj.prjId,
                            fileName: info.key,
                            url: domain + info.key
                        };
                        curPrj
                            .postMonitorPointsConfig(uploadItem)
                            .then(function() {
                                curPrj.monitorTypes = [];
                                curPrj.monitorPointsDictionary = [];
                                getDictionary();
                                $scope.status = "succeeded";
                            }, function() {
                                $scope.status = "failed";
                            });
                    },
                    'Error': function(up, err, errTip) {
                        alert('上传出错，刷新页面再试！');
                    },
                }
            });
            return uploader;
        };
        $timeout(initUploader, 400);
    }]);
});
